//
//  CompanySelectViewController.m
//  CityFire
//
//  Created by liuyangzhu on 2017/8/16.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "CompanySelectViewController.h"
#import "CompanySelectTableViewCell.h"
#import "DeviceViewModel.h"

@interface CompanySelectViewController () <UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) DeviceViewModel *deviceViewModel;

@end

@implementation CompanySelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = kCompanySelect_TITLE;
    
    self.tableView.tableFooterView  = [UIView new];
    
    [self bindRAC];
    [self.deviceViewModel getQueryPersonOrgs:nil];
}

- (void)bindRAC
{
    [RACObserve(self.deviceViewModel, personOrgsArray) subscribeNext:^(NSArray *orgsArray) {
        if (orgsArray) {
            [self.tableView reloadData];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.deviceViewModel.personOrgsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CompanySelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CompanySelectTableViewCell"];
    
    DeviceModel *deviceModel = self.deviceViewModel.personOrgsArray[indexPath.row];
    cell.companyLabel.text = deviceModel.deviceOrg;
    cell.locationLabel.text = deviceModel.deviceAddress;
    
    return cell;
}

-  (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DeviceModel *deviceModel = self.deviceViewModel.personOrgsArray[indexPath.row];

    if (self.selectedOrgnameBlock) {
        self.selectedOrgnameBlock(deviceModel.deviceOrg);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSString* keyword = [[self.searchBar text] stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (!keyword.length) {
        [SVProgressHUD showInfoWithStatus:@"请填写搜索单位名称"];
        [searchBar becomeFirstResponder];
        
        return;
    }
    
//    [self.deviceViewModel getAllDeviceInfo:@{@"pageNo" : @"", @"pageSize" : @""}];
    [self.deviceViewModel getQueryPersonOrgs:nil];
    
    [searchBar resignFirstResponder];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    searchBar.text = @"";
    searchBar.text = @"";
    
    [searchBar resignFirstResponder];
    searchBar.showsCancelButton = NO;
}

- (void)searchBar:(UISearchBar *)searchBar
selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
    
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = NO;
}

- (DeviceViewModel *)deviceViewModel
{
    if (_deviceViewModel == nil) {
        _deviceViewModel = [[DeviceViewModel alloc] init];
    }
    
    return _deviceViewModel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
